package com.card323.crpg.cmdhandler;

import java.awt.Color;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.card323.crpg.Hero;
import com.card323.crpg.item.Item;

public class ShopCommandHandler extends CommandHandler {

	public ShopCommandHandler(Hero hero) {
		super(hero);

	}

	@Override
	public void handle(String cmd) {
		handle_shop_info(cmd);
		handle_buy_item(cmd);

	}

	private void handle_shop_info(String cmd) {
		if (cmd.matches("(\\s)*(|\\?)s(|hop)(\\s)*")) {
			_hero.send_message(_hero.get_shop().get_shop_items_info());
		}
	}

	private void handle_buy_item(String cmd) {
		String item_buy_regex = "(\\s)*s(|hop)(\\s)*b(|uy)(\\s)*([0-9]+)(\\s)*";
		Pattern pattern = Pattern.compile(item_buy_regex);
		Matcher matcher = pattern.matcher(cmd);
		if (matcher.find()) {

			int item_index = Integer.valueOf(matcher.group(6));
			Item item = _hero.get_shop().get_item(item_index-1);
			if (_hero._money < item.get_price()) {
				_hero.send_message("金币不足 道具[" + item_index + "]" + item._name
						+ " 价格:" + item.get_price() + "英雄持有金币:" + _hero._money,
						Color.RED);
				return;
			}
			if (_hero.is_package_full()) {
				_hero.send_message("背包已满", Color.RED);

				return;
			}

			if (CommandHelper.sure(_hero, "确定购买道具[" + item_index + "]"
					+ item._name + " 价格:" + item.get_price() + "?")) {

				_hero.send_message("购买道具[" + item_index + "]" + item._name,
						Color.BLUE);
				_hero.buy_item(item);
			}
		}
	}

}
